/*
 * @Author: dlb
 * @Date: 2020-07-03 11:07:31
 * @LastEditTime: 2020-07-03 14:27:51
 * @LastEditors: dlb
 * @Description:
 * @FilePath: \zhufeng-cli-project\my-cli2\src\main.js
 * @可以输入预定的版权声明、个性签名、空行等
 */
import program from 'commander'
import { VERSION } from './utils/constants'

let actionMap = {
    install: {//配置命令的名字
        alias: 'i',
        description: 'install template',//命令对应的描述
        examples: [ // 命令对应的模板
            'my-cli i',
            'my-cli install'
        ]
    },
    config: {
        alias: 'c',
        description: 'config .myclirc',
        examples: [
            'my-cli config set <k> <v>',
            'my-cli config get <k>',
            'my-cli config romove <k>'
        ]
    },
    '*': {
        description: 'not found',
        examples: []
    }
}
// Object.keys() 方法会返回一个由一个给定对象的自身可枚举属性组成的数组，数组中属性名的排列顺序和正常循环遍历该对象时返回的顺序一致
Object.keys(actionMap).forEach(action => {
    program.command(action)
        .description(actionMap[action].description)
        .alias(actionMap[action].alias)
        .action(() => {
            console.log(action)
            //判断一下当前用的是什么操作
            if(action === 'config'){
                //实现更改配置文件
                //console.log(process.argv)//数组
                main(action,process.argv.slice(3));
            }else if(action === 'install'){

            }
            main()
    })
})
function help(){
    // console.log('123')//把example显示出去
    console.log('\r\n  '+'how to use command');
    Object.keys(actionMap).forEach(action=>{
        actionMap[action].examples.forEach(example=>{
            console.log('  - '+example)
        })
    })
}
program.on('-h',help);
program.on('--help',help);
program.version(VERSION, '-v --version').parse(process.argv);